import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Form, Tabs } from 'antd';
import classNames from 'classnames';
import LoginItem from "./LoginItem";
import LoginTab from "./LoginTab";
import LoginSubmit from "./LoginSubmit";
import styles from "./index.less?modules";

var Login = /*#__PURE__*/function (_Component) {
  _inherits(Login, _Component);

  var _super = _createSuper(Login);

  function Login() {
    var _this;

    _classCallCheck(this, Login);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      type: _this.props.defaultActiveKey,
      tabs: [],
      active: {}
    };

    _this.onSwitch = function (type) {
      _this.setState({
        type: type
      });

      _this.props.onTabChange(type);
    };

    _this.handleSubmit = function (e) {
      e.preventDefault();
      var _this$state = _this.state,
          active = _this$state.active,
          type = _this$state.type;
      var activeFileds = active[type];

      _this.props.form.validateFields(activeFileds, {
        force: true
      }, function (err, values) {
        _this.props.onSubmit(err, values);
      });
    };

    return _this;
  }

  _createClass(Login, [{
    key: "getChildContext",
    value: function getChildContext() {
      var _this2 = this;

      return {
        tabUtil: {
          addTab: function addTab(id) {
            _this2.setState({
              tabs: [].concat(_toConsumableArray(_this2.state.tabs), [id])
            });
          },
          removeTab: function removeTab(id) {
            _this2.setState({
              tabs: _this2.state.tabs.filter(function (currentId) {
                return currentId !== id;
              })
            });
          }
        },
        form: this.props.form,
        updateActive: function updateActive(activeItem) {
          var _this2$state = _this2.state,
              type = _this2$state.type,
              active = _this2$state.active;

          if (active[type]) {
            active[type].push(activeItem);
          } else {
            active[type] = [activeItem];
          }

          _this2.setState({
            active: active
          });
        }
      };
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          className = _this$props.className,
          children = _this$props.children;
      var _this$state2 = this.state,
          type = _this$state2.type,
          tabs = _this$state2.tabs;
      var TabChildren = [];
      var otherChildren = [];
      React.Children.forEach(children, function (item) {
        if (!item) {
          return;
        } // eslint-disable-next-line


        if (item.type.__ANT_PRO_LOGIN_TAB) {
          TabChildren.push(item);
        } else {
          otherChildren.push(item);
        }
      });
      return /*#__PURE__*/React.createElement("div", {
        className: classNames(className, styles.login)
      }, /*#__PURE__*/React.createElement(Form, {
        onSubmit: this.handleSubmit
      }, tabs.length ? /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Tabs, {
        animated: false,
        className: styles.tabs,
        activeKey: type,
        onChange: this.onSwitch
      }, TabChildren), otherChildren) : _toConsumableArray(children)));
    }
  }]);

  return Login;
}(Component);

Login.defaultProps = {
  className: '',
  defaultActiveKey: '',
  onTabChange: function onTabChange() {},
  onSubmit: function onSubmit() {}
};
Login.childContextTypes = {
  tabUtil: PropTypes.object,
  form: PropTypes.object,
  updateActive: PropTypes.func
};
Login.Tab = LoginTab;
Login.Submit = LoginSubmit;
Object.keys(LoginItem).forEach(function (item) {
  Login[item] = LoginItem[item];
});
export default Form.create()(Login);